package com.pactera.madp.cp.controller.sidebar.product;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.dto.product.ProductEmployeeGroupUpdateDto;
import com.pactera.madp.cp.api.dto.product.ProductEmployeeUpdateBackImgDTO;
import com.pactera.madp.cp.api.em.product.ProductLevelEnum;
import com.pactera.madp.cp.api.entity.WorkEmployeeEntity;
import com.pactera.madp.cp.api.entity.product.ProdEmployeeEntity;
import com.pactera.madp.cp.api.vo.product.ProductDailVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.service.IWorkEmployeeService;
import com.pactera.madp.cp.service.ProdEmployeeGroupService;
import com.pactera.madp.cp.service.ProdEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 产品经理表(CpProdEmployee)表控制层
 *
 * @author yushen.qi
 * @since 2021-12-17 16:19:55
 */

@RestController
@AllArgsConstructor
@RequestMapping("/product-employee-wechat")
@Api(value = "product", tags = "产品经理管理-小程序")
public class SidebarProdEmployeeController {

    /**
     * 服务对象
     */
    @Resource
    private ProdEmployeeService prodEmployeeService;
    @Resource
    private IWorkEmployeeService employeeService;
    @Resource
    private ProdEmployeeGroupService prodEmployeeGroupService;

    /**
     * 分页查询所有数据
     *
     * @param wxUserId           用户id
     * @return 所有数据
     */
    @GetMapping("/vo")
    @ApiOperation(value = "分页查询产品经理信息")
    public R selectOne(@RequestParam String wxUserId) {
        Integer corpId = CorpContextHolder.getCorpId();
        WorkEmployeeEntity workEmployeeInfo = employeeService.getWorkEmployeeInfo(wxUserId);
        ProductDailVO productDailVO = new ProductDailVO();
        ProdEmployeeEntity one = prodEmployeeService.getOne(Wrappers.<ProdEmployeeEntity>lambdaQuery().eq(ProdEmployeeEntity::getEmployeeId, wxUserId).eq(ProdEmployeeEntity::getCorpId, corpId));
        if(one==null){
            productDailVO.setAvatar(workEmployeeInfo.getAvatar());
            productDailVO.setBackImg("/mfs/logo.png");
            productDailVO.setBannerImg("/mfs/remind.png");
            productDailVO.setState(1);
        }else{
            BeanUtil.copyProperties(one,productDailVO);
            productDailVO.setEmployeeId(wxUserId);
            productDailVO.setEmployeeName(one.getEmployeeName());
            productDailVO.setPhone(one.getPhone());
            productDailVO.setBackImg(one.getBackImg());
            productDailVO.setAvatar(workEmployeeInfo.getAvatar());
            productDailVO.setLevel(ProductLevelEnum.getTypeByCode(one.getUserLevel()));
            if(productDailVO.getState()==1){
                productDailVO.setLevel(null);
                productDailVO.setPhone(null);
            }
        }
        return R.ok(productDailVO);
    }

    @SysLog("改变logo及广告样式")
    @PutMapping("/logo")
    @ApiOperation(value = "改变logo及广告样式")
    public R change(@RequestBody ProductEmployeeUpdateBackImgDTO dto){
        boolean update = prodEmployeeService.update(null, new UpdateWrapper<ProdEmployeeEntity>()
                .set(StrUtil.isNotBlank(dto.getLogUrl()),"back_img", dto.getLogUrl())
                .set(StrUtil.isNotBlank(dto.getBannerUrl()),"banner_img",dto.getBannerUrl())
                .eq("employee_id", dto.getEmployeeId())
                .eq("corp_id", CorpContextHolder.getCorpId()));
        return update?R.ok():R.failed();
    }

    @SysLog("自定义产品组样式")
    @PutMapping("/img")
    @ApiOperation(value = "自定义产品组样式")
    public R changeImg(@RequestBody ProductEmployeeGroupUpdateDto dto){
        return prodEmployeeGroupService.put(dto) ? R.ok() : R.failed();
    }

}
